<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { fetchIndicesData, fetchIndicesData3day } from '../api/weatherAPI'
import { useCityStore } from '../stores/cityStore'

const cityStore = useCityStore()
const route = useRoute()
const id = ref('')
const detailData = ref(null)
const futureIndices = ref([]) // 新增变量存储未来3天数据
const resid = [
  '运动指数',
  '洗车指数',
  '穿衣指数',
  '钓鱼指数',
  '紫外线指数',
  '旅游指数',
  '过敏指数',
  '舒适度指数',
  '感冒指数',
  '空气污染扩散条件指数',
  '空调开启指数',
  '太阳镜指数',
  '化妆指数',
  '晾晒指数',
  '交通指数',
  '防晒指数',
]
onMounted(async () => {
  id.value = route.params.id
  if (cityStore.location.id) {
    const res = await fetchIndicesData(cityStore.location.id, id.value)
    detailData.value = res
  }
  try {
    const futureRes = await fetchIndicesData3day(cityStore.location.id, id.value)
    futureIndices.value = futureRes.daily || []
  } catch (error) {
    console.error('获取未来指数数据失败:', error)
    futureIndices.value = []
  }
})
</script>
<template>
  <div class="living-index-detail">
    <h1>生活指数详情页面：{{ resid[id - 1] }}</h1>
    <div class="detail-content">
      <div class="forecast" v-if="detailData?.daily">
        <ul>
          <li v-for="(day, index) in detailData.daily" :key="index">
            <p>{{ day.category }}</p>
            <p>{{ day.text }}</p>
          </li>
        </ul>
      </div>
    </div>

    <div class="future-forecast" v-if="futureIndices.length > 0">
      <h2>未来3天预报</h2>
      <div class="forecast-days">
        <div v-for="(day, index) in futureIndices" :key="index" class="forecast-day">
          <div class="date-info">
            <p class="weekday">{{ day.weekday }}</p>
            <p class="date">{{ day.date }}</p>
          </div>
          <p class="category">{{ day.category }}</p>
          <p class="text">{{ day.text }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.living-index-detail {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  background-color: #f5f5f5;
  border-radius: 1rem;
  color: black;
}

.detail-content {
  margin-top: 1.5rem;
}

.description {
  margin: 1rem 0;
  line-height: 1.6;
}

.forecast {
  margin-top: 2rem;
}

.forecast ul {
  list-style: none;
  padding: 0;
}

.forecast li {
  background-color: #fff;
  border-radius: 0.5rem;
  padding: 1rem;
  margin-bottom: 1rem;
}

@media (max-width: 768px) {
  .living-index-detail {
    padding: 1rem;
  }
}
.forecast-days {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.forecast-day {
  flex: 1;
  min-width: 200px;
  background-color: #fff;
  border-radius: 0.8rem;
  padding: 1.2rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.date-info {
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #eee;
}

.weekday {
  font-size: 1.2rem;
  font-weight: bold;
  color: #2980b9;
}

.date {
  font-size: 0.9rem;
  color: #7f8c8d;
  margin-top: 0.2rem;
}

@media (max-width: 768px) {
  .living-index-detail {
    padding: 1rem;
  }

  .forecast-days {
    flex-direction: column;
  }

  .forecast-day {
    min-width: 100%;
  }
}
</style>

